package userInterface;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class WelcomeFrame {
	
	private JFrame welcomeFrame;
	
	//Welcoming frame of the application
    public void go() {

    	//Create the frame
		welcomeFrame = new JFrame();
		welcomeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		welcomeFrame.setResizable(false);
		welcomeFrame.setTitle("Welcome");
		
		//Create the bar menu
		JMenuBar menuWelcome = new JMenuBar();
		JMenu file = new JMenu("File");
	    JMenuItem createMenuItem = new JMenuItem("Create a new game");
	    createMenuItem.addActionListener(new FrameListener());
	    file.add(createMenuItem);
	    JMenuItem quitMenuitem = new JMenuItem("Quit");
	    quitMenuitem.addActionListener(new QuitListener());
	    file.add(quitMenuitem);
	    
	    JMenu help = new JMenu("Help");
	    JMenuItem helpMenuItem = new JMenuItem("Read me");
	    help.add(helpMenuItem);
	    
		menuWelcome.add(file);
	    menuWelcome.add(help);
	    
	    //Create the buttons
        JButton quit = new JButton("Quit");
        quit.addActionListener(new QuitListener());
		JButton create = new JButton("Create a new game");
	    welcomeFrame.getRootPane().setDefaultButton(create);
		create.addActionListener(new FrameListener());
		
		//Display the text and the picture
		JLabel titleAndInfo = new JLabel();
		String infoString = "<html> " +
								"<br />" +
								"<br />" +
								"<p align=\"center\"><font size=\"5\" color=\"#272F78\"><b>Welcome to AI Soccer Project</font></b></p><br /><br /><br /><br /><br />" +
								"<p align=\"center\">version 1.0</p> " +
								"<p align=\"center\">ENSEEIHT, INP Toulouse</p>" +
								"<p align=\"center\">A. Voinot, F. Bughin, A. Gehin</p>" +
							"</html>";
		titleAndInfo.setText(infoString);
		titleAndInfo.setHorizontalAlignment(SwingConstants.CENTER);
		JLabel soccerBallImage = new JLabel(new ImageIcon(getClass().getResource("soccer-ball.png")));

		//Create a panel containers and add the components
        JPanel panelContainerCenter = new JPanel();
        JPanel panelContainerWest = new JPanel();
        JPanel panelContainerSouth = new JPanel();
        JPanel buttonPanel = new JPanel();
       
        buttonPanel.add(quit); 
        buttonPanel.add(create);
        panelContainerSouth.setLayout(new BorderLayout());
        panelContainerCenter.add(titleAndInfo);
        panelContainerWest.add(soccerBallImage);
        panelContainerSouth.add(buttonPanel, BorderLayout.EAST);
 
        //Set the frame parameters
        welcomeFrame.setJMenuBar(menuWelcome);
        welcomeFrame.getContentPane().add(BorderLayout.CENTER, panelContainerCenter);
        welcomeFrame.getContentPane().add(BorderLayout.WEST, panelContainerWest);
        welcomeFrame.getContentPane().add(BorderLayout.SOUTH, panelContainerSouth);
        
		welcomeFrame.setSize(600, 338);
		welcomeFrame.setLocationRelativeTo(null);
		welcomeFrame.setVisible(true);
    }
    
    //action performed when you click on or select create new simulation
    class FrameListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
	      	SimulationFrame simulationFrame = new SimulationFrame();
	      	simulationFrame.go();
	      	welcomeFrame.dispose();
		}
	}
    
    //action performed when you click on or select quit
    class QuitListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			System.exit(0);
		}
	}
    
}
